package com.ldzy.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;


@PropertySource("classpath:db.properties")
public class JdbcConfig {
    //作用:读取数据库配置参数,生成数据源对象,并添加到spring容器中
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.pwd}")
    private String pwd;
    @Value("${jdbc.myDriver}")
    private String driver;

    @Bean
    public DataSource getDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(pwd);
        ds.setDriverClassName(driver);
        return ds;
    }

    //事务管理器
//    @Bean
//    public PlatformTransactionManager transactionManager(DataSource dataSource){
//        DataSourceTransactionManager ds = new DataSourceTransactionManager();
//        ds.setDataSource(dataSource);
//        return ds;
//    }
}
